import os
import random
import numpy as np
import time

def delay(seconds):
    print(f"Delaying for {seconds} seconds...")
    time.sleep(seconds)
    print("Delay complete!")

def count_files(path):
    """
    统计指定路径下的文件数量。
    
    参数:
    path -- 要统计的路径。
    
    返回:
    文件的数量。
    """
    count = 0
    for root, dirs, files in os.walk(path):
        count += len(files)
    return count
def read_image_names(folder_path):
    valid_extensions = ['.tif', '.tiff', '.png', '.jpg', '.TIF']
    image_names = []
    for file in os.listdir(folder_path):
        if os.path.splitext(file)[1].lower() in valid_extensions:
            image_names.append(file)
    return image_names

def write_result_txt(test_image_path, base_map_path, output_path):
    test_images = read_image_names(test_image_path)
    base_maps = read_image_names(base_map_path)
    
    # 创建 NumPy 数组
    result_array = []
    for test_image in test_images:
        base_map = np.random.choice(base_maps)
        result_array.append([test_image, base_map, 0, 0, 1000, 1000])

    with open(os.path.join(output_path, 'RESULT.txt'), 'w') as file:
        for test_image in test_images:
            base_map = random.choice(base_maps)
            file.write(f"{test_image} {base_map} 0 0 1000 1000\n")

if __name__ == "__main__":
    import sys
    
    # if len(sys.argv) != 4:
    #     print("Usage: python run.py input_path output_path")
    # else:
    input_path = sys.argv[1]
    output_path = sys.argv[2]
    print("sys.argv",sys.argv)
    print(input_path,output_path)
    test_image_path = os.path.join(input_path, 'test_image')
    base_map_path = os.path.join(input_path, 'base_map')

    if not os.path.exists(test_image_path) or not os.path.exists(base_map_path):
        print("Error: Invalid input paths.")
    else:
        test_image_count = count_files(test_image_path)
        base_map_count = count_files(base_map_path)

        print(f"测试图像文件数量: {test_image_count}")
        print(f"基础地图文件数量: {base_map_count}")

        write_result_txt(test_image_path, base_map_path, output_path)
        print("RESULT.txt generated successfully./")
    # delay(522.36)